/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  XESHelper.java                                      
 *  File Contents Outline :  XES持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbXES;
import storage.util.StorageManager;

import storage.dbobject.DbxesNameTable;


public class XESHelper {

    /**
     * 将xes保存到LaUDMS数据库中
     *
     * @param xes 要存储的模型对象
     */
    public void saveXES(DbXES xes) {
      //  StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
            PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        sm.setRowKey(xes.getRowKey());
        sm.put(DbXES.cl_name, xes.getName());
        sm.put(DbXES.cl_xes, xes.getXES());
        sm.put(DbXES.cl_createTime, xes.getCreateTime());
        sm.put(DbXES.cl_branch, xes.getBranch());
        System.out.println(xes.getBranch());
        sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xesNameTable"));
        sm.setRowKey(xes.getName());
        sm.put(DbxesNameTable.xesName, "1");
        /*
        ArrayList list = getAllXESName();
        if(!list.contains(xes.getName()))
            sm.put(DbxesNameTable.xesName, xes.getName());
            * 
            */
        sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
        sm.setRowKey(xes.getRowKey());
        if (xes.getClassify() != null) {
            if (xes.getClassify().length() > 0) {
                sm.put(DbXES.cl_classify, xes.getClassify());
            }
        }
        System.out.println("save XES:" + xes.getName());
//        sm.close();
    }

    /**
     * 将一系列xes保存到LaUDMS数据库中
     *
     * @param xeses 要存储的模型对象列表
     */
    public void saveXESs(List<DbXES> xeses) {
     //   StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        for (int i = 0; i < xeses.size(); i++) {
            DbXES xes = xeses.get(i);
            sm.setRowKey(xes.getRowKey());
            sm.put(DbXES.cl_name, xes.getName());
            sm.put(DbXES.cl_xes, xes.getXES());
            sm.put(DbXES.cl_createTime, xes.getCreateTime());
            sm.put(DbXES.cl_branch, xes.getBranch());
            if (xes.getClassify() != null) {
                if (xes.getClassify().length() > 0) {
                    sm.put(DbXES.cl_classify, xes.getClassify());
                }
            }
        }
//        sm.close();
    }

    public List<DbXES> getXESs(final String columnName, final String value) {
        List<DbXES> result = new LinkedList<DbXES>();
    //    StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String branch = null, name = null, xes = null, createTime = null, classify = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbXES.cl_branch)) {
                        branch = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_xes)) {
                        xes = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_createTime)) {
                        createTime = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_classify)) {
                        classify = columnsValue;
                    }
                }
                DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime, classify);
                result.add(pXES);
            }
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public List<DbXES> getXESsByNameBranch(final String columnName, final String value, final String columnName1, final String value1) {
        List<DbXES> result = new LinkedList<DbXES>();
   //     StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        List<KeySlice> crs = sm.getByTwoColumn(columnName, value, columnName1, value1);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String branch = null, name = null, xes = null, createTime = null, classify = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbXES.cl_branch)) {
                        branch = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_xes)) {
                        xes = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_createTime)) {
                        createTime = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_classify)) {
                        classify = columnsValue;
                    }
                }
                DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime, classify);
                result.add(pXES);
            }
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public List<String> getBranchListByName(final String columnName, final String value, final String columnName1) {
        List<String> result = new LinkedList();
     //   StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        List<KeySlice> crs = sm.getAllInOneColumnByAnother(DbXES.cl_name, value, DbXES.cl_branch);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String branch = null, name = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbXES.cl_branch)) {
                        branch = columnsValue;
                    } else if (columnsName.equals(DbXES.cl_name)) {
                        name = columnsValue;
                    }
                }
                // DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime);
                if (!result.contains(branch)) {
                    result.add(branch);
                }
            }
            Collections.sort(crs);//sort the name of branch
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }
 public List<String> getAllXESNameByClassify(final String columnName, final String value, final String columnName1) {
         List<String> results = new LinkedList();
   //     StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
          PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        List<KeySlice> crs = sm.getAllInOneColumnByAnother(columnName, value, columnName1);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String  result = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(columnName1)) {
                        result = columnsValue;
                    }
                }
                // DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime);
                if (!results.contains(result)) {
                    results.add(result);
                }
            }
            Collections.sort(crs);//sort the name of branch
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return results;
    }
    public ArrayList<String> getAllXESName() {
     //   StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xesNameTable"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        //      List<KeySlice> crs = sm.getAll();
        ArrayList<String> xesNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = "";
                //for (ColumnOrSuperColumn column : ks.getColumns()) {
                    //name = new String(ks.getColumns().get(0).getColumn().getValue());
                //}
                if(ks.getColumns().size()!=0)
                {
                    name = new String(ks.getKey());

                    xesNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return xesNameList;
    }

    public Hashtable<String, Integer> getSameNameNum(final String columnName, final String name) {
        // System.out.println("getSameNameNum");
  //      StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        //   List<KeySlice> crs = sm.getAllInOneColumn(DbXES.cl_name);
        Hashtable<String, Integer> branchInfo = new Hashtable<String, Integer>();
        int branchNum = 0;
        String branch = String.valueOf(branchNum);
        List<KeySlice> crs = sm.getByTwoColumn(columnName, name, DbXES.cl_branch, branch);
        try {

            while (crs.size() > 0) {
                if (branchInfo.containsKey(branch)) {
                    Integer newNum = new Integer(branchInfo.get(branch).intValue() + 1);
                    branchInfo.put(branch, newNum);
                    //  System.out.println(" branchInfo.put:" + branch + " newNum:" + newNum);
                } else {
                    branchInfo.put(branch, new Integer(1));
                    //   System.out.println(" branchInfo.put:" + branch + "  new Integer(1):");
                }
                branchNum++;
                branch = String.valueOf(branchNum);
                crs = sm.getByTwoColumn(columnName, name, DbXES.cl_branch, branch);
            }
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return branchInfo;
    }

    public Hashtable<String, Integer> getSameNameNumOld(final String columnName, final String name) {
        // System.out.println("getSameNameNum");
      //  StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        //   List<KeySlice> crs = sm.getAllInOneColumn(DbXES.cl_name);
        List<KeySlice> crs = sm.get(columnName, name);
        Hashtable<String, Integer> branchInfo = new Hashtable<String, Integer>();
        try {
            for (KeySlice ks : crs) {
                boolean match = false;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbXES.cl_name) && columnsValue.equalsIgnoreCase(name)) {
                        match = true;

                        break;
                    }
                }
                if (match) {
                    //   System.out.println("  match = true:" + name);
                    for (ColumnOrSuperColumn column : ks.getColumns()) {
                        String columnsName = new String(column.getColumn().getName());
                        String columnsValue = new String(column.getColumn().getValue());

                        //  if (match) {
                        if (columnsName.equals(DbXES.cl_branch)) {
                            //  System.out.println(" columnsName.equals(DbXES.cl_branch)" );
                            if (branchInfo.containsKey(columnsValue)) {
                                Integer newNum = new Integer(branchInfo.get(columnsValue).intValue() + 1);
                                branchInfo.put(columnsValue, newNum);
                                //  System.out.println(" branchInfo.put:" + columnsValue + " newNum:" + newNum);
                            } else {
                                branchInfo.put(columnsValue, new Integer(1));
                                //   System.out.println(" branchInfo.put:" + columnsValue + "  new Integer(1):" );
                            }
                        }
//                        else{
//                         System.out.println("!columnsName.equals(DbXES.cl_branch)"+ DbXES.cl_branch+"  "+columnsName);}
                    }
                }
//                else {
//                    System.out.println("  match = false:" + name);
//                }


            }
        } catch (Exception ex) {
            Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return branchInfo;
    }

    public void deleteXES(final String rowKey) {
    //    StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_xes);
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_xes"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }
      public static void main(String args []){
        XESHelper xesh = new XESHelper();
        List<String> xxh =   xesh.getAllXESName();
        List<String> test2 = new ArrayList<String>();
         System.out.println("xes count"+xxh.size());
        HashMap<String,Integer> counter = new HashMap<String,Integer>();
        for( String xxh1 : xxh ){
        	System.out.println(xxh1);
        }
     
     //System.out.println("count:"+count);
        
        
    }
}
